import {Injectable} from "@angular/core";
import {OAuthService} from "angular-oauth2-oidc";

import  {v4 as uuid}  from "uuid";


@Injectable({providedIn: 'root'})
export class MyOAuthService extends OAuthService {
  protected override storeAccessTokenResponse(
    accessToken: string,
    refreshToken: string,
    expiresIn: number,
    grantedScopes: string,
    customParameters?: Map<string, string>
  ) {
    super.storeAccessTokenResponse(
      accessToken,
      refreshToken,
      expiresIn,
      grantedScopes,
      customParameters
    );
    this.notifyStorageUpdate();
  }


  // @ts-ignore
  public override logOut(): void{
    super.logOut();
    this.notifyStorageUpdate();
  }
  // @ts-ignore
  public override logOut(customParameters: boolean | object) : void{
    super.logOut(customParameters);
    this.notifyStorageUpdate();
  }
  // @ts-ignore
  public override logOut(noRedirectToLogoutUrl: boolean) : void{
    super.logOut(noRedirectToLogoutUrl);
    this.notifyStorageUpdate();
  }
  // @ts-ignore
  public override logOut(noRedirectToLogoutUrl: boolean, state: string) : void{
    super.logOut(noRedirectToLogoutUrl);
    this.notifyStorageUpdate();
  }


  private notifyStorageUpdate() {
    this._storage.setItem('auth_data_updated', uuid());
    this._storage.removeItem('auth_data_updated');
  }
}
